package com.sjc.algorithm.job;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 抢红包，二倍均值法
 * @author by SangJiacun
 * @Date 2020/5/14 16:33
 */
public class RedPackage {

    public static void main(String[] args) {
        List<Integer> amountList = divideRedPackage(1000, 10);
        amountList.stream().forEach(amount -> System.out.println(amount));
        System.out.println("total -> "+amountList.stream().mapToInt(Integer::intValue).sum());
    }

    private static List<Integer> divideRedPackage(Integer totalAmout, Integer totalPeopleNum){
        List<Integer> amountList = new ArrayList<>();
        Integer restAmout = totalAmout;
        Integer restPeopleNum = totalPeopleNum;

        Random random = new Random();
        for(int i=0;i< totalPeopleNum-1;i++){
            int amount = random.nextInt(restAmout/restPeopleNum * 2 -1)+1;
            restAmout = restAmout-amount;
            restPeopleNum--;
            amountList.add(amount);
        }
        amountList.add(restAmout);
        return amountList;
    }
}
